var wl = require("../components/winLocation.js");
var returnUrl;
$(function() {
    
    var $retrunurl = $("#retrunurl");
    var $url = $("#url");
    if($retrunurl.val() && $retrunurl.val() != ""){
       returnUrl = $retrunurl.val(); 
    }
    if($url.val() && $url.val() != ""){
        returnUrl = $url.val();
    }
    if (!(sessionStorage.returnUrl) && !(returnUrl)) {
        hideCountDown();
    } else {
        countDown();
    }


    setH2();
    $("#ok").bind("touchstart", function() {
        if(returnUrl){
            location.href = returnUrl;
        }else{
            location.href = sessionStorage.returnUrl; 
        }
        
    });
    pushHistoryUrl();
});

function countDown() {
    var count = 10;
    var theHandle = setInterval(function() {
        count--;
        $("#djs").text(count);
        if (count == 0) {
            clearInterval(theHandle);
            if (returnUrl) {
                location.href = returnUrl;
            } else if (sessionStorage.returnUrl && sessionStorage.returnUrl != "") {
                location.href = sessionStorage.returnUrl;
            } else {
                history.go(-4);
                window.opener = null;
                window.close();
            }

        }
    }, 1000);
}

// 没有上送将隐藏倒计时 和确定按钮
function hideCountDown() {

    $(".msg_desc").hide();
    $(".opr_area").hide();
}

function setH2() {
    wl.QueryString.Initial();
    var to = wl.QueryString.GetValue("time");
    ts_amt = wl.QueryString.GetValue("ts_amt");
    var erroVal = wl.QueryString.GetValue("ev");
    var msg = wl.QueryString.GetValue("msg");
    var errorMsg = sessionStorage.getItem("errorMsg");

    if (to == "out") {
        $("h2.msg_title").text("订单已失效");
    } else if (erroVal == "orderErro") {
        $("h2.msg_title").text("支付失败,请重新下订单");
    } else if (erroVal == "paid") {
        var msg = decodeURI(wl.QueryString.GetValue("msg"));
        $("h2.msg_title").text("该笔订单已支付");
    } else if (msg == "failure") {
        $("h2.msg_title").text("该笔订单已失效");
    } else if (msg == "empty") {
        $("h2.msg_title").text("该笔订单不存在");
    } else if(msg){
        $("h2.msg_title").text(decodeURI(msg));
    } else if(errorMsg){
        $("h2.msg_title").text(errorMsg);
    }

    if (ts_amt && $("#ts_amt")[0]) {
//        $("#ts_amt").text(ts_amt);
    }
}

// 增加历史浏览记录
function pushHistoryUrl(argument) {
    var scUrl = "";
    if (sessionStorage.returnUrl) {
        scUrl = sessionStorage.returnUrl
    } else if ($("#url").val()) {
        scUrl = $("#url").val()
    }

    history.pushState({
        title: "支付网关"
    }, "商城", scUrl);
}